// src/utils/auth.js
import request from './request'

class AuthManager {
    /**
     * 验证用户是否已登录
     * @returns {Promise<boolean>} 返回是否已登录
     */
    static async checkLoginStatus() {
        try {
            const response = await request.get('/user/info')
            return response.status === 200
        } catch (error) {
            return false
        }
    }

    /**
     * 获取用户信息
     * @returns {Promise<Object>} 用户信息
     */
    static async getUserInfo() {
        try {
            const response = await request.get('/user/info')
            return response.data
        } catch (error) {
            throw new Error('获取用户信息失败')
        }
    }

    /**
     * 用户登录
     * @param {string} username 用户名
     * @param {string} password 密码
     * @returns {Promise<Object>} 登录结果
     */
    static async login(username, password) {
        try {
            const response = await request.post('/auth/login', {username, password})
            return response.data
        } catch (error) {
            throw new Error('登录失败')
        }
    }

    /**
     * 用户登出
     * @returns {Promise<void>}
     */
    static async logout() {
        try {
            await request.post('/auth/logout')
        } catch (error) {
            console.error('登出失败:', error)
        }
    }
}

export default AuthManager
